//
//  TLMainTableViewCell.swift
//  TLiOS26LiquidGlassDemo
//
//  Created by jiayin on 2025/7/11.
//
import UIKit
import SnapKit

class TLMainTableViewCell: UITableViewCell {
    
//    // 创建UILabel来显示title和details
//    private let titleLabel: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 16)
//        label.translatesAutoresizingMaskIntoConstraints = false
//        return label
//    }()
//    
//    private let detailsLabel: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.translatesAutoresizingMaskIntoConstraints = false
//        return label
//    }()
//    
//    // 创建属性来存储TLMainModel实例
//    var model: TLMainModel? {
//        didSet {
//            updateUI()
//        }
//    }
//    
//    static func height(for model: TLMainModel) -> CGFloat {
//        // 根据模型数据计算并返回单元格高度
//        return 60.0 // 示例值，实际值应根据模型内容动态计算
//    }
//    
//    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        setupUI()
//        // 设置背景色
//        contentView.backgroundColor = .placeholderText // 或者其他颜色
//    }
//    
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//    
//    private func setupUI() {
//        // 添加UILabel到contentView
//        contentView.addSubview(titleLabel)
//        contentView.addSubview(detailsLabel)
//        
//        // 使用SnapKit设置布局约束
//        titleLabel.snp.makeConstraints { make in
//            make.leading.equalTo(contentView.snp.leading).offset(16)
//            make.trailing.equalTo(detailsLabel.snp.leading).offset(-10) // 调整左右间距
//            make.centerY.equalTo(contentView.snp_centerYWithinMargins);
//        }
//        
//        detailsLabel.snp.makeConstraints { make in
//            make.leading.equalTo(titleLabel.snp.trailing).offset(10)
//            make.trailing.equalTo(contentView.snp.trailing).offset(-16)
//            make.centerY.equalTo(titleLabel.snp.centerY)
//        }
//    }
//    
//    private func updateUI() {
//        // 更新UILabel的文本
//        titleLabel.text = model?.title
//        detailsLabel.text = model?.details
//    }
    
    // 创建UILabel来显示title和details
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 18) // 调整字体大小
        label.textColor = .black // 设置字体颜色
        label.translatesAutoresizingMaskIntoConstraints = false
        label.setContentHuggingPriority(.defaultLow, for: .horizontal)
        return label
    }()
    
    private let detailsLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16) // 调整字体大小
        label.textColor = .gray // 设置字体颜色
        label.translatesAutoresizingMaskIntoConstraints = false
        label.setContentHuggingPriority(.defaultLow, for: .horizontal)
        return label
    }()
    
    // 创建UIImageView来显示右方向箭头
    private let arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(systemName: "chevron.right")
        imageView.tintColor = .black
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.setContentHuggingPriority(.defaultLow, for: .horizontal)
        return imageView
    }()
    
    // 创建属性来存储TLMainModel实例
    var model: TLMainModel? {
        didSet {
            updateUI()
        }
    }
    
    static func height(for model: TLMainModel) -> CGFloat {
        // 根据模型数据计算并返回单元格高度
        return 50.0 // 调整高度以适应新的布局
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        // 设置背景色和圆角
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
        // 可选：添加边框
//        contentView.layer.borderWidth = 1
//        contentView.layer.borderColor = UIColor.lightGray.cgColor
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 添加UILabel到contentView
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailsLabel)
        contentView.addSubview(arrowImageView)
        
        // 使用SnapKit设置布局约束
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(contentView.snp.leading).offset(30) // 增加左边距
            make.trailing.equalTo(detailsLabel.snp.leading).offset(-20)
            make.centerY.equalTo(contentView.snp.centerY);
        }
        
        detailsLabel.snp.makeConstraints { make in
            make.leading.equalTo(titleLabel.snp.trailing).offset(20)
            make.trailing.equalTo(contentView.snp.trailing).offset(-50) // 增加右边距
            make.centerY.equalTo(titleLabel.snp.centerY)
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.leading.equalTo(detailsLabel.snp.trailing).offset(20)
            make.trailing.equalTo(contentView.snp.trailing).offset(-20)
            make.centerY.equalTo(contentView.snp.centerY)
        }
    }
    
    private func updateUI() {
        // 更新UILabel的文本
        titleLabel.text = model?.title
        detailsLabel.text = model?.details
    }
}
